package com.efounder.constant;

import java.util.HashMap;
import java.util.Map;

import com.efounder.util.AppContext;
import com.efounder.util.StorageUtil;

public class EnvironmentVariable {

    private static Map<String, Object> evMap = new HashMap<String, Object>();
    private static StorageUtil storageutil = new StorageUtil(AppContext.getInstance(), "EnvironmentVariable");

    // 用户名
    public static String getUserName() {
        return (String) get("kUserName");
    }

    public static void setUserName(String userName) {
        set("kUserName", userName);
    }

    // email
    public static String getEmail() {
        return (String) get("kEmail");
    }

    public static void setEmail(String email) {
        set("kEmail", email);
    }

    // ID
    public static String getUserID() {
        return (String) get("kUserID");
    }

    public static void setUserID(String userID) {
        set("kUserID", userID);
    }

    // 密码
    public static String getPassword() {
        return (String) get("kPassword");
    }

    public static void setPassword(String password) {
        set("kPassword", password);
    }

    // MD5Email
    public static String getMD5Email() {
        return (String) get("kMD5Email");
    }

    public static void setMD5Email(String mD5Email) {
        set("kMD5Email", mD5Email);
    }

    // kMD5UserID
    public static String getMD5UserID() {
        return (String) get("kMD5UserID");
    }

    public static void setMD5UserID(String mD5UserID) {
        set("kMD5UserID", mD5UserID);
    }

    // kMD5PassWord
    public static String getMD5PassWord() {
        return (String) get("kMD5PassWord");
    }

    public static void setMD5PassWord(String mD5PassWord) {
        set("kMD5PassWord", mD5PassWord);
    }

    // kmD5
    public static String getMD5() {
        return (String) get("kMD5");
    }

    public static void setMD5(String mD5) {
        set("kMD5", mD5);
    }

    // kPlatform
    public static String getPlatform() {
        return (String) get("kPlatform");
    }

    public static void setPlatform(String platform) {
        set("kPlatform", platform);
    }

    // kMenuSelectedID
    public static String getMenuSelectedID() {
        return (String) get("kMenuSelectedID");
    }

    public static void setMenuSelectedID(String menuSelectedID) {
        set("kMenuSelectedID", menuSelectedID);
    }

    // kDeviceToken
    public static String getDeviceToken() {
        return (String) get("kDeviceToken");
    }

    public static void setDeviceToken(String deviceToken) {
        set("kDeviceToken", deviceToken);
    }

    // kServerURL
    public static String getServerURL() {
        return (String) get("kServerURL");

    }

    public static void setServerURL(String serverURL) {
        set("kServerURL", serverURL);
    }

    // kVersionPath
    public static String getVersionPath() {
        return (String) get("kVersionPath");
    }

    public static void setVersionPath(String versionPath) {
        set("kVersionPath", versionPath);
    }

    // 自定义
    public static String getProperty(String key, String defaultValue) {
        String value = (String) get(key);
        if (value == null) value = defaultValue;
        return value;
    }

    // 自定义
    public static String getProperty(String key) {
        return getProperty(key, null);
    }

    public static void setProperty(String key, String value) {
        set(key, value);
    }

    // kF_DWMC
    public static String getF_DWMC() {
        return (String) get("kF_DWMC");
    }

    public static void setF_DWMC(String f_DWMC) {
        set("kF_DWMC", f_DWMC);
    }

    // F_DWBH
    public static String getF_DWBH() {
        return (String) get("kF_DWBH");
    }

    public static void setF_DWBH(String f_DWBH) {
        set("kF_DWBH", f_DWBH);
    }

    // F_CHDATE
    public static String getF_CHDATE() {
        return (String) get("kF_CHDATE");
    }

    public static void setF_CHDATE(String f_CHDATE) {
        set("kF_CHDATE", f_CHDATE);
    }

    // F_ZGBH
    public static String getF_ZGBH() {
        return (String) get("kF_ZGBH");
    }

    public static void setF_ZGBH(String f_ZGBH) {
        set("kF_ZGBH", f_ZGBH);
    }

    // F_CRDATE
    public static String getF_CRDATE() {
        return (String) get("kF_CRDATE");
    }

    public static void setF_CRDATE(String f_CRDATE) {
        set("kF_CRDATE", f_CRDATE);
    }

    // 权限
    @SuppressWarnings("unchecked")
    public static Map<String, Object> getPowerMap() {
        return (Map<String, Object>) evMap.get("kF_PowerMap");
    }

    public static void setPowerMap(Map<String, Object> f_powerMap) {
        evMap.put("kF_PowerMap", f_powerMap);
    }


    private static void set(String key, Object value) {
        evMap.put(key, value);
        storageutil.putString(key, (String) value);
        storageutil.commit();
    }

    private static Object get(String key) {
        String mapValue = (String) evMap.get(key);
        if (mapValue == null) {
            mapValue = storageutil.getString(key);
            if (mapValue == null || "".equals(mapValue)) {
                mapValue = null;
            }
            evMap.put(key, mapValue);
        }
        return mapValue;
    }

    public static void saveMap(Map<String, String> propertyMap) {
        if (propertyMap != null) {
            for (String key : propertyMap.keySet()) {
                storageutil.putString(key, (String) propertyMap.get(key));
            }
            storageutil.apply();
        }
    }
}
